'use strict';

angular.module('app')
    .controller('dmManagerController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal','$window',
        function ($scope, $rootScope, $state, $timeout, toaster,$modal,$window) {
            $scope.title = '设备列表';
            $scope.param = {};
            $scope.openparam = {};
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                if ($scope.pageFlag != 1) {//未点击翻页
                    $scope.param.pageNum = 1;//将页码设置为1，即跳转回第一页
                } else {//点击翻页
                    $scope.pageFlag = 2;//将开关值设置为2
                }
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/dac/dm/dmManager/list',
                    data: angular.toJson($scope.param)
                }).then(function (result) {
                    $scope.loading = false;
                    console.log(result);
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }
            //从本地获取localstorage数据，对权限按钮做隐藏
            var personAuth = localStorage.getItem("PersonAuth");//获取PersonAuth的值
            personAuth = personAuth.split(',');
//            console.log(personAuth);
            for (var i = 0; i < personAuth.length; i++) {
                if (personAuth[i] == 'dm.deviceInfo.read') {
                    $scope.read = true;
                }
                if (personAuth[i] == 'dm.deviceInfo.add') {
                    $scope.add = true;
                }
                if (personAuth[i] == 'dm.deviceInfo.update') {
                    $scope.update = true;
                }
                if (personAuth[i] == 'dm.deviceInfo.delete') {
                    $scope.dele = true;
                }
            }
            $scope.search();
            $scope.delDevice = function (deviceId, deviceCode) {
                if (confirm('确定要删除设备' + '[' + deviceCode + ']' + '吗?')) {
                    $scope.delParam = {};
                    $scope.loading = true;
                    $scope.delParam.deviceId = deviceId;
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/dac/dm/dmManager/delete',
                        data: angular.toJson($scope.delParam)
                    }).then(function (result) {
                        if (result.httpCode == 200) {
                            $scope.loading = false;
                            $scope.search();
                        } else if (result.httpCode == 667) {
                        	toaster.pop('error', '', '已有授权信息关联该设备，不能删除!');
                        }else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                    });
                }
            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum = page;
                $scope.pageFlag = 1;//将开关值设置为1
                $scope.search();
            };

            $scope.deviceOpen = function (item) {
            	if(item.buildingStatus != 0){//判断楼栋状态是否为禁用
//                	$scope.openparam.msg = "7E46432D38393130413137303430313032FFFFFFFF7115425B0303000000000401010000837E";
                    $scope.openparam.msg = "7E46432D38393130413137303430303039FFFFFFFF7115425B0303000000000401000000887E";
                    $scope.openparam.devId = item.onenet_device_id;
                    $scope.openparam.devKey = item.onenetKey;
                    $scope.openparam.type = "webopen";

                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: '/hwapi/hw/api',
                        data: angular.toJson($scope.openparam)
                    }).then(function (result) {
                        if (result.httpCode == 200) {
                            toaster.clear('*');
                            toaster.pop('success', '', "请求成功");
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', result.msg);
                        }
                    });
            	}else{
            		toaster.clear('*');
                    toaster.pop('error', '', '设备所在楼栋状态为无效，无法开门。');
            	}
            }
            
            $scope.addAuth = function (item) {
                $scope.authparam.devId = item.onenet_device_id;
                $scope.authparam.devKey = item.onenetKey;
                $scope.authparam.devCode = item.deviceCode;

                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    url: '/hwapi/hw/authapi',
                    data: angular.toJson($scope.authparam)
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        toaster.clear('*');
                        toaster.pop('success', '', "请求成功");
                    } else {
                        toaster.clear('*');
                        toaster.pop('error', result.msg);
                    }
                });

            }
            
            //替换设备模态框
			$scope.changeDevice = function(deviceId) {
				var modalInstance = $modal.open({
					templateUrl: 'changeDevice.html', // script标签中定义的id
					controller: 'changeDeviceCtrl', // modal对应的Controller
					size: 'lg',
					resolve: {
						items: function() {
							return deviceId;
						}
					}
				});
	
				modalInstance.opened.then(function() { // 模态窗口打开之后执行的函数
	
				});
				modalInstance.result.then(function(result) {
					$scope.record.productId = result[0];
					$scope.record.productName = result[1];
					$scope.record.registerCode = result[2];
					$scope.record.masterApikey = result[3];
				},function(reason) {
					console.log(reason);
				});
			};
}]);
        
//替换设备模态框控制器
app.controller('changeDeviceCtrl', function($scope,$modal, $modalInstance,toaster,$window,items) {
	$scope.title = '设备替换'
	$scope.param = {};
	$scope.record = {}
	$scope.searchDvice = function() {
		$.ajax({
			type: 'PUT',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: '/dac/dm/product/list',
			data: angular.toJson($scope.param)
		}).then(function(result) {
			console.log(result);
			if(result.httpCode == 200) {
				$scope.pageInfo = result.data;
			} else {
				$scope.msg = result.msg;
			}
			$scope.$apply();
		});
	}
	$scope.searchDvice();
	$scope.hiden = function(){
		$scope.deviceCodeNULL = false;
		$scope.productIdNULL = false;
	}
	
	$scope.saveDevice = function() {
		
		$scope.record.deviceId = items//模态框带过来的原设备id
		if($scope.record.deviceCode==null){
			$scope.deviceCodeNULL = true;
			return;
		}else if($scope.record.productId==null){
			$scope.productIdNULL = true;
			return;
		}else{
			$.ajax({
			type: 'post',
			dataType: 'json',
			contentType: 'application/json;charset=UTF-8',
			url: '/dac/dm/device/replace',
			data: angular.toJson($scope.record)
		}).then(function(result) {
			if(result.httpCode == 200){
				toaster.clear("*");
				toaster.pop('success','','替换成功');
				$window.location.reload();
				$scope.cancel();
			}else{
				toaster.clear("*");
				toaster.pop('error','','替换失败');
			}
			$scope.$apply();
			
		});
		}
		console.log($scope.record);
		
	}
	$scope.resetFrom = function(){
		$scope.record = {};
	}
	
	$scope.cancel = function() {
		$modalInstance.dismiss('cancel');
	};
});